﻿Imports <xmlns:log="urn:myLogConfig">
Imports System.IO

Public Class cheminUC

    Friend monChemin As Chemin
    Friend treeViewSender As TreeViewItemWithIcons
    Friend r As MainWindow

    Private Sub BTN_save_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles BTN_save.Click

        Try

            'Rechercher le noeud du chemin
            Dim chmXML = (From ele In r.masterDoc...<log:chemin>
                            Select ele Where ele.Parent.Parent.@nom = monChemin.srvParent.pltParent.nom And ele.Parent.@nom = monChemin.srvParent.nom And ele.@destdossier = monChemin.destDossier).First

            chmXML.@destdossier = TBX_dossierDestination.Text
            chmXML.Value = TBX_source.Text

            If rdb_typeLogDossiers.IsChecked Then
                chmXML.@typelogs = cst.typeLogs.dossiers
            Else
                chmXML.@typelogs = cst.typeLogs.fichiers
            End If


            Me.monChemin.reFill(chmXML)


            'Mettre à jour l'arbre
            Me.treeViewSender.Tag = monChemin
            Me.treeViewSender.HeaderText = monChemin.destDossier

            'Sauvegarder les changement dans le fichier
            r.masterDoc.Save(r.xmlFileName)

            'Afficher message succes
            r.consoleOut.addItim(New ConsoleTypes.ok("Modification du chemin: Succès", chmXML.@destdossier))

        Catch ex As Exception
            r.consoleOut.addItim(New ConsoleTypes.erreur("Une erreur est apparue lors de la modification du chemin", ex.Message))
        End Try
    End Sub

    Friend Sub setInfo(ByRef chm As Chemin, sender As TreeViewItemWithIcons)

        monChemin = chm
        treeViewSender = sender

        TBX_dossierDestination.Text = monChemin.destDossier
        TBX_source.Text = monChemin.source

        If monChemin.typeLogs = cst.typeLogs.dossiers Then
            rdb_typeLogDossiers.IsChecked = True
        Else
            rdb_typeLogFichiers.IsChecked = True
        End If

    End Sub

    Private Sub BTN_cheminDelete_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles BTN_cheminDelete.Click
        If MessageBox.Show("Supprimer le chemin, continuer ?", "Confirmation", MessageBoxButton.YesNo) = MessageBoxResult.Yes Then

            Try
                'Rechercher le noeud du chemin
                Dim queryChm = (From ele In r.masterDoc...<log:chemin>
                                Select ele Where ele.Parent.Parent.@nom = monChemin.srvParent.pltParent.nom And ele.Parent.@nom = monChemin.srvParent.nom And ele.@destdossier = monChemin.destDossier).First


                queryChm.Remove()
                r.panelDynamic.Children.Clear()

                'Sauvegarder les changement dans le fichier
                r.masterDoc.Save(r.xmlFileName)

                'recharger treeView
                r.readXML()

                'Afficher message succes
                r.consoleOut.addItim(New ConsoleTypes.ok("Suppression du chemin: Succès", queryChm.@destdossier & " supprimé"))

            Catch ex As Exception
                r.consoleOut.addItim(New ConsoleTypes.erreur("Une erreur est apparue lors de la suppression du chemin", ex.Message))
            End Try
        End If
    End Sub


    Private Sub TBX_source_TextChanged(sender As System.Object, e As System.Windows.Controls.TextChangedEventArgs) Handles TBX_source.TextChanged
        rtb_source.Document.Blocks.Clear()

        'prefixe
        Dim block_prefix As New Run("\\" & monChemin.srvParent.ip & "\")
        block_prefix.Foreground = Windows.Media.Brushes.DarkGray
        block_prefix.FontWeight = FontWeights.Bold

        'suffixe
        Dim block_dossierDest As New Run(TBX_source.Text)

        Dim papra As New Paragraph()
        papra.Inlines.Add(block_prefix)
        papra.Inlines.Add(block_dossierDest)

        rtb_source.Document.Blocks.Add(papra)

        LBL_testSourceResult.Content = ""
    End Sub


    Private Sub TBX_destination_TextChanged(sender As System.Object, e As System.Windows.Controls.TextChangedEventArgs) Handles TBX_dossierDestination.TextChanged
        rtb_destination.Document.Blocks.Clear()

        'prefixe
        Dim block_prefix As New Run(monChemin.srvParent.pltParent.destination & "\" & monChemin.srvParent.pltParent.nom & "\" & monChemin.srvParent.nom & "\")
        block_prefix.Foreground = Windows.Media.Brushes.DarkGray
        block_prefix.FontWeight = FontWeights.Bold
        'suffixe
        Dim block_dossierDest As New Run(TBX_dossierDestination.Text)
        'ajout au paragraph
        Dim papra As New Paragraph()
        papra.Inlines.Add(block_prefix)
        papra.Inlines.Add(block_dossierDest)
        'ajout au richtextbox
        rtb_destination.Document.Blocks.Add(papra)


        LBL_testDestResult.Content = ""
    End Sub

    Private Sub btn_testSource_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btn_testSource.Click
        LBL_testSourceResult.Foreground = Brushes.Black : LBL_testSourceResult.Content = "Test en cours..." : r.DoEvents()

        Try
            Dim pltParent = monChemin.srvParent.pltParent
            Using New Impersonator(pltParent.login, pltParent.domain, pltParent.password)
                pathManager.testFullAccessDirectory(rtb_source.GetText())
            End Using

            LBL_testSourceResult.Foreground = Brushes.Green : LBL_testSourceResult.Content = "OK"
            r.consoleOut.addItim(New ConsoleTypes.msg("Le test d'accès en lecture/ecriture au repertoire source a réussi", "Chemin: " & rtb_source.GetText()))
        Catch ex As Exception
            r.consoleOut.addItim(New ConsoleTypes.erreur("Les test d'accès au répertoire source a echoué", ex.Message))
            LBL_testSourceResult.Foreground = Brushes.Red : LBL_testSourceResult.Content = "Erreur"
        End Try
    End Sub

    Private Sub btn_testDestination_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btn_testDestination.Click
        LBL_testDestResult.Foreground = Brushes.Black : LBL_testDestResult.Content = "Test en cours..." : r.DoEvents()

        Try
            Dim credArchivage = r.masterDoc...<log:credArchivage>

            Using New Impersonator(credArchivage.@login, credArchivage.@domain, credArchivage.@password)
                pathManager.testFullAccessDirectory(rtb_destination.GetText())
            End Using

            LBL_testDestResult.Foreground = Brushes.Green : LBL_testDestResult.Content = "OK"
            r.consoleOut.addItim(New ConsoleTypes.msg("Le test d'accès en lecture/ecriture au repertoire destination a réussi", "Chemin: " & rtb_source.GetText()))
        Catch ex As Exception
            r.consoleOut.addItim(New ConsoleTypes.erreur("Les test d'accès au répertoire destination a echoué", ex.Message))
            LBL_testDestResult.Foreground = Brushes.Red : LBL_testDestResult.Content = "Erreur"
        End Try
    End Sub

    Private Sub UserControl_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
        r = MainWindow.moiMeme

        TBX_dossierDestination.RaiseEvent(New TextChangedEventArgs(Controls.TextBox.TextChangedEvent, UndoAction.Undo))
        TBX_source.RaiseEvent(New TextChangedEventArgs(Controls.TextBox.TextChangedEvent, UndoAction.Undo))
    End Sub



End Class
